(function(){
    const url = '/hello';
    const result = document.querySelector('.result');
    const fetchGetBtn = document.querySelector('.fetch-get');
    console.log( fetchGetBtn )
    fetchGetBtn.addEventListener('click', ()=>{
        // fetch( `${url}?name=阿芳` );
        let promise = fetch( `${url}?name=阿芳`, { method: 'GET'});
        promise.then( response => {
            let plain = response.text();
            console.log( plain );
            return plain;
        }).then(data => {
            console.log( data );
            result.innerHTML = data;
        }).catch( error => {
            console.log( error );
        })
    },false);

    const fetchPostBtn = document.querySelector('.fetch-post');
    fetchPostBtn.addEventListener('click', ()=>{
        let promise = fetch( url, {
            method: 'POST',
            headers: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            body: 'name=阿芳'
        });
        promise.then( response => {
            let res = response.json();
            console.log( res );
            return res;
        }).then( data => {
            console.log( data );
            result.innerHTML = JSON.stringify(data);
        }).catch( error => {
            console.log( error );
        })
    },false);
})();
